package com.tongxin.van.frameworkcanary.view;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.tongxin.van.frameworkcanary.MainActivity;
import com.tongxin.van.frameworkcanary.R;
import com.tongxin.van.frameworkcanary.base.BaseActivity;
import com.tongxin.van.frameworkcanary.databinding.ActivityLoginBinding;
import com.tongxin.van.frameworkcanary.http.Api;
import com.tongxin.van.frameworkcanary.http.HttpResult;
import com.tongxin.van.frameworkcanary.http.HttpUtil;
import com.tongxin.van.frameworkcanary.utils.UserUtil;


import java.util.HashMap;
import java.util.Map;

/**
 *
 * author: VanyLiu
 * email: 13212674795@163.com
 * description:登录页面
 *
**/
public class LoginActivity extends BaseActivity<ActivityLoginBinding> {

    private static final String TAG = LoginActivity.class.getSimpleName();

    @Override
    protected int layoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void getBundleData(Bundle bundle) {
    }

    @Override
    protected void initTitlebar() {


    }

    @Override
    protected void initViews() {
        //todo 测试账号
//        mBinding.etPhone.setText("18028066112");
//        mBinding.etPassword.setText("123456789");
        mBinding.btnLogin.setOnClickListener(v -> {
            onLogin();
        });



        initEditText();
    }

    private void initEditText() {
        //手机号码输入框
        mBinding.etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().length() > 0) {
                    mBinding.ivPhoneEmpty.setVisibility(View.VISIBLE);
                } else {
                    mBinding.ivPhoneEmpty.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //清空手机号码输入框
        mBinding.ivPhoneEmpty.setOnClickListener(v -> {
            mBinding.etPhone.setText("");
        });

        //密码输入框
        mBinding.etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().length() > 0) {
                    mBinding.ivPwdEmpty.setVisibility(View.VISIBLE);
                } else {
                    mBinding.ivPwdEmpty.setVisibility(View.GONE);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        //清空密码输入框
        mBinding.ivPwdEmpty.setOnClickListener(v -> {
            mBinding.etPassword.setText("");
        });
    }

    /**
     * 登录
     */
    private void onLogin() {
        String phone = mBinding.etPhone.getText().toString();
        String password = mBinding.etPassword.getText().toString();
//        if (!RegexUtils.isMobileSimple(phone)) {
//            ToastUtils.showShort("手机号码有误");
//            return;
//        }
//        if (TextUtils.isEmpty(password)) {
//            ToastUtils.showShort("密码不能为空");
//            return;
//        }
        //        mBinding.etPhone.setText("18028066112");
//        mBinding.etPassword.setText("123456789");
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", "18028066112");
        map.put("password", "123456789");
        new HttpUtil(this, true)
                .api(Api.getApiService().onLogin(map))
                .call(new HttpResult() {
                    @Override
                    protected void onSuccess(String json) {
                        //结果解析
                        ToastUtils.showShort("登录成功");
                        Log.e("json:", json);
                        Intent intent = new Intent();
                        intent.setClass(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    protected void onFail(String message) {
                        ToastUtils.showShort(message);
                    }
                });
    }

    @Override
    protected void loadData() {


    }

    @Override
    public void onBackPressed() {
        // 下面这个 setResult 是用于购物车fragment带放回值跳转
        Intent intent = new Intent();
        intent.putExtra("login", "login_fail");
        setResult(RESULT_OK, intent);
        super.onBackPressed();
    }

    /**
     * 点击空白位置 隐藏软键盘
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (null != this.getCurrentFocus()) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            return mInputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);
        }
        return super.onTouchEvent(event);
    }
}
